package cm.iccsoft.android.telecomcost.entities;

import java.util.ArrayList;

import android.util.Log;

public class Country extends Entity {
	public Country() {
		_type = PROP_TYPE;
	}

	/**
	 * @param zone_id
	 * @param code
	 */
	public Country(long zone_id, String code) {
		super();
		this.zone_id = zone_id;
		this.code = code;
	}

	private long zone_id;

	private String code;

	private ArrayList<Provider> _providers = new ArrayList<Provider>();

	public void setCode(String value) {
		this.code = value;
	}

	public String getCode() {
		return code;
	}

	public void setProviders(ArrayList<Provider> value) {
		this._providers = value;
	}

	public ArrayList<Provider> getProviders() {
		return _providers;
	}	

	public String toString(boolean idOnly) {
		if (idOnly) {
			return String.valueOf(getCode());
		} else {
			StringBuffer sb = new StringBuffer();
			sb.append("Country[ ");
			sb.append("Name=").append(getName()).append(" ");
			sb.append("Code=").append(getCode()).append(" ");
			sb.append("Provider.size=").append(getProviders().size())
					.append(" ");
			sb.append("]");
			return sb.toString();
		}
	}

	public void setZoneId(long zone_id) {
		this.zone_id = zone_id;
	}

	public long getZoneId() {
		return zone_id;
	}

	public static final String PROP_TYPE = "Country";

	public boolean contains(Provider _provider) {
		int index = 0;

		while (index < _providers.size()) {
			Provider provider = _providers.get(index);
			if (provider.getId() == _provider.getId()) {
				return true;
			}
			
			index++;
		}

		return false;
	}
	
	public boolean add(Provider _provider)
	{
		if(remove(_provider))
		{
			_providers.add(_provider);
		}		
		
		return contains(_provider);
	}

	public boolean remove(Provider _provider) {
		Log.i("ProviderListActivity", "removing provider " + _provider.getName() + " from country " + getName());
		ArrayList<Provider> newProviders = new ArrayList<Provider>();

		int index = 0;

		while (index < _providers.size()) {
			Provider provider = _providers.get(index);
			if (provider.getId() != _provider.getId()) {
				newProviders.add(provider);
			}

			index++;
		}

		_providers = newProviders;
		
		return !contains(_provider);
	}	
}
